package com.molichuxing.services.infrastructure.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.molichuxing.framework.dto.Paged;
import com.molichuxing.services.infrastructure.bean.AccidentBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.AccidentMapper;
import com.molichuxing.services.infrastructure.dto.request.create.AccidentCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.AccidentModifyDto;
import com.molichuxing.services.infrastructure.dto.response.AccidentDownloadDto;
import com.molichuxing.services.infrastructure.dto.response.AccidentDto;
import com.molichuxing.services.infrastructure.service.AccidentService;


/**
 * <p>
 * 事故 服务实现类
 * </p>
 *
 * @author
 * @since 2019-08-15
 */
@Service("accidentService")
public class AccidentServiceImpl implements AccidentService {

  private static final Logger log = Logger.getLogger(AccidentServiceImpl.class);

  @Resource
  private AccidentMapper accidentMapper;

  @Override
  public Paged<AccidentDto> getCarAccidentPage(Map<String, Object> map, Integer pageNum, Integer pageSize) {
      Paged<AccidentDto> paged = new Paged<>();
      //paged.setTab(getTabList(map));
      int count = accidentMapper.getAccidentPageCount(map);
      if(count <= 0) {
          return paged;
      }
      List<AccidentBean> result = accidentMapper.getAccidentPage(map, pageNum, pageSize);
      log.info("根据条件查询事故列表 出参：result： "+ JSON.toJSONString(result));
      paged.setList(Convert.toAccidentBeanDto(result));
      paged.setTotal(count);
      paged.setPageSize(pageSize);
      return paged;
  }

    @Override
    public List<AccidentDto> getAccidentByCarId(Integer carId) {
        return Convert.toAccidentBeanDto(accidentMapper.getAccidentByCarId(carId));
    }

    @Override
    public AccidentDto getCarAccident(Integer accidentId) {
        return Convert.toAccidentBeanDto(accidentMapper.getById(accidentId));
    }

    @Override
    public Boolean modifyAccident(AccidentModifyDto modifyDto) {
      int result = accidentMapper.modifyAccident(Convert.toAccidentBean(modifyDto));
      if(result > 0) {
          return true;
      }
      return false;
    }

    @Override
    public Paged<AccidentDto> getCarAccidentAppPage(List<Long> orderCodes, Integer userId, Integer pageNum, Integer pageSize) {
        Paged<AccidentDto> paged = new Paged<>();

        int count = accidentMapper.getAccidentAppPageCount(orderCodes, userId, pageNum, pageSize);
        if(count <= 0) {
            return paged;
        }
        List<AccidentBean> result = accidentMapper.getAccidentAppPage(orderCodes, userId, pageNum, pageSize);
        paged.setList(Convert.toAccidentBeanDto(result));
        paged.setPageSize(pageSize);
        paged.setTotal(count);
        paged.setPageSize(pageSize);
        return paged;
    }

    @Override
    public Integer createAccident(AccidentCreateDto accidentCreateDto) {
        AccidentBean accidentBean = Convert.toAccidentBean(accidentCreateDto);
        accidentMapper.createAccident(accidentBean);
        return accidentBean.getId();
    }

    @Override
    public int updateAccidentById(AccidentCreateDto accidentCreateDto) {
        return accidentMapper.updateAccidentById(Convert.toAccidentBean(accidentCreateDto));
    }

    @Override
    public int updateAccidentStatusById(Integer accidentId, Integer status) {
        return accidentMapper.updateAccidentStatusById(accidentId,status);
    }

    /**
     *  事故待确认数量& 结案待确认数量
     * @param status
     * @return
     */
    @Override
    public Integer getAccidentCount(Integer status) {
        return accidentMapper.getAccidentCount(status);
    }

    @Override
    public int getCarAccidentTabCount(Map<String, Object> map) {
        return accidentMapper.getAccidentPageCount(map);
    }
    
    /**
     * 查询事故报备导出数据
     *    * ids id集合
     *    * licencePlate 车牌号
     *    * vin 车架号
     *    * userId 用户id
     *    * liabilityType 责任类型（0：无责，1：全责，2：主责，3：同责，4：次责）
     *    * isCasualty 是否有人伤（0：无人伤，1：有人伤）
     *    * accidentType 事故类型（1:单方事故，2：双方事故，3：多方事故，4：停放受损，5：车辆盗抢，6：车辆自燃，7：车辆涉水，8：重大自然灾害）
     *    * accidentStartTime     出险开始时间
     *    * accidentEndTime     出险结束时间
     *    * status 事故状态（1：事故未确认，2：事故已确认，3：结案未确认，4：已结案，5：无效报备）
     * @param map
     * @return
     */
    @Override
    public List<AccidentDownloadDto> getAccidentDownload(Map<String, Object> map) {
		return accidentMapper.getAccidentDownload(map);
	}

	/**
	 * 查询事故报备导出数据条数
	 * 
	 * @param selectParams
	 * @return
	 */
	@Override
	public Long getAccidentDownloadCount(Map<String, Object> selectParams) {
		return accidentMapper.getAccidentDownloadCount(selectParams);
	}	


}
